<template>
	<view class="Loin_content">
		<view class="header" >
			<view class="pwd_back" >
				<image src="../../static/image/pwd_back_icon.png" @tap="back"></image>
			</view>
			<text class="login_text"  >{{i18n.Forgetpassword}}</text>
			
			<view class="input_content accoutName" :style="i18n.wjmmaccoutName"><input type="text"
			v-model="accountName" 
			:placeholder="i18n.drqbzhulicitext"
			placeholder-class="input_placeholder"
			style="width: calc(100% - 80upx);"
			/></view>
			<!-- <checkbox-group @change="check_xiaobai" class="xiaobai_check">
				<label >
						<checkbox value="1"  color="#FFCC33" style="transform:scale(0.7)" />我是小白用户(小白用户必选)
				</label>
			</checkbox-group> -->
			<view class="xiaobai_account">{{i18n.sjzhm}}：oexchainofficial.{{accountName}}</view>
			<!-- 密码 -->
			<view class="input_content newpwd  password_style" :style="i18n.wjmmaccoutpsw">
				<input :type="eye?'text':'password'"
				v-model="password" 
				:placeholder="i18n.pswtext"
				placeholder-class="input_placeholder"
				/>
				<image
				:src="eye?'../../static/image/openeye.png':'../../static/image/closeeye.png'" 
				@tap="chageEye(1)"
				class="eyes"></image>
			</view>
			<view class="input_content repwd password_style" :style="i18n.wjmmaccoutzpsw">
				<input :type="eye2?'text':'password'" 
				v-model="password2"
				:placeholder="i18n.xiaobaibg5placeholder"
				placeholder-class="input_placeholder"
				/>
				<image
				:src="eye2?'../../static/image/openeye.png':'../../static/image/closeeye.png'" 
				@tap="chageEye(2)"
				class="eyes"></image>
			</view>
			<!-- 下一步 -->
			<image class="login_btn next_btn2"  :src="changeColor?i18n.psw2imgbuttong:i18n.psw2imgbutton" @tap="next"></image>
		</view>
		<!-- 修改提示 -->
		<view class="endit_tips">
			<text>{{i18n.endit_tips1}}：</text>
			<text>{{i18n.endit_tips2}}</text>
			<text>{{i18n.endit_tips3}}</text>
			<text>{{i18n.endit_tips4}}</text>
		</view>
	</view>
</template>

<script>
	import xflSelect from '../../components/xfl-select/xfl-select.vue'; 
	 import * as oex from 'oex-web3'; 
	import { ethers } from 'ethers';
	import CryptoJS from 'crypto-js';
	import Files from '../../common/files.js'
	import EthCrypto from 'eth-crypto';
	
	
	export default {
		data() {
			return {
				// list: [       //要展示的数据
				// 		'苹果',
				// 		{value: '香蕉', disabled: true},
				// 		'葡萄',
				// 		'芒果',
				// 		'大白菜',
				// ],
				
				accountName:"", //要绑定的账号
				password:"",
				password2:"",
				//check:[],//小白Checkbox
				Wallet:"",
				mobile:"",
				valcode:"",
				result:"",
				changeColor:false,
				publicKey:"",
				eye:false,
				eye2:false
			}
		},
		components: { xflSelect },
		onHide() {
			this.accountName = "";
			this.password = "";
			this.password2 = "";
		},
		onLoad(options) {
			console.log(options)
			this.valcode = options.valcode;
			this.mobile = options.mobile;
			
		},
		computed: {
			changeData() {
				const { accountName, password,password2} = this
				return {
					accountName,
					password,
					password2
				}
			},
			i18n() {
				return this.$t('user')
			}
		},
		watch: {
			changeData: {
				handler: function (newval, oldval) {
					this.accountName = newval.accountName;
					this.password = newval.password;
					this.password2 = newval.password2;
					if(this.accountName != "" && this.password != "" && this.password2 != ""){
						this.changeColor = true
					}else{
						this.changeColor = false
					}
				},
				deep: true
			}
		},
		methods: {
			chageEye(n){
				if(n == 1){
					this.eye = !this.eye;
				}else{
					this.eye2 = !this.eye2;
				}
				
			},
			// check_xiaobai(e){
			// 	console.log(e.detail.value)
			// 	this.check = e.detail.value;
			// },
			selectChange(val){
				//选择要绑定的账号
				this.accountName = val.newVal;
				
			},
			register(){
				this.$CommonJS.navigateTo('../register/register')
			},
			async next(){
				const _this = this;
				//validate input
				if(this.accountName == ""){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入要绑定的账号':'Please enter the account to bind');
					return;
				}
				if(!this.$CommonJS.accountValidate(this.accountName)){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '账号名不符合规范':'The account name does not conform to the specification');
					return;
				}
				if(!this.password){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入新密码':'Please enter a new password');
					return;
				}
				if(!this.$CommonJS.passwordValidate(this.password)){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '密码请输入8-16位数字字母组合':'Please input 8-16 digit alphanumeric combination for password');
					return;
				}
				if(!this.password2){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '请输入确认密码':'Please enter the confirmation password');
					return;
				}
				if(this.password !== this.password2){
					this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '两次输入的密码不一致':'The two passwords are not the same');
					return;
				}
				
				// if(this.check.length < 1){
				// 	this.$CommonJS.showToast("一级账户无法修改密码");
				// 	return;
				// }
				//console.log(this.check);
				
				//查看是否存在这个账号
				uni.showLoading({
					// title:"密码重置中"
					title:_this._i18n.locale == 'zh_CN' ? '密码重置中':'Password reset in progress',
				})
				oex.utils.setApp();
				//let accountName = this.accountName;
				// if(this.check.length >0){
				// 	//小白账户a
				// 	accountName =  this.$API.xiaobai + this.accountName;
				// }
				const accountName =  this.$API.xiaobai + this.accountName.trim();
				
				var data = await oex.account.getAccountExByName(accountName);
				
				const nodeInfo = uni.getStorageSync('nodeInfo');
			 await this.$CommonJS.request(nodeInfo,"POST",data).then( res => {
						console.log(res)
						if(res.data.result == null || res.data.result == "" || res.data.result == undefined){
							_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '账号不存在':'Account does not exist');
							
							return;
						}
						_this.result = res.data.result;
						
							
				
					
				}).catch(error => {
						console.log(error)
				})
				
				
				//生成新的钱包
				const identity = EthCrypto.createIdentity();
				const  Wallet = await  new ethers.Wallet( identity.privateKey ) //创建一个新的随机钱包
				//console.log(Wallet);
				this.Wallet = {
					privateKey:Wallet.privateKey,
					publicKey:Wallet.publicKey,
					address:Wallet.address,
					_isSigner:Wallet._isSigner
				}
				this.publicKey = Wallet.publicKey;
						// const  Wallet = await ethers.Wallet.createRandom(); //创建一个新的随机钱包
						// console.log(Wallet);
						//_this.Wallet = Wallet;
						//const privateKey = Wallet.privateKey;  //私钥
						//_this.privateKey = Wallet.privateKey;
						
						//账号存在更新账户信息
						
						uni.request({
							url:_this.$API.appServeApi + _this.$API.resetpasswordval + '?i18n_language=' + uni.getStorageSync('system_info'),
							method:"POST",
							header: {
								'content-type': 'application/x-www-form-urlencoded' //自定义请求头信息
							},
							data:{
								mobile:_this.mobile,
								valcode:_this.valcode,
								account:_this.result.accountName,
								publickey:_this.publicKey,
							},
							success(res) {
								console.log(res)
								if(res.data.code == 200){
									//对称加密
									var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(_this.Wallet), _this.password).toString();
									const xiaobai = _this.$API.xiaobai + _this.accountName + '.txt';
									//	写入文件
									Files.fileWriter(ciphertext,xiaobai).then(txt =>{
										console.log(txt)
										if(txt.status == 200){
											uni.hideLoading();
											uni.showToast({
												// title:"修改成功",
												title:res.data.message,
												icon:"none",
												duration:2000,
												success() {
													var setTimeOut=setTimeout(function(){
													    	_this.$CommonJS.navigateTo('../login/login')
													},2000);
												
												}
											})
											// uni.showModal({
											//     title: res.data.message,
											//     content: '修改密码',
											//     success: function (res) {
											//         if (res.confirm) {
											//             console.log('用户点击确定');
											// 													_this.$CommonJS.navigateTo('../login/login')
											//         } else if (res.cancel) {
											//             console.log('用户点击取消');
											//         }
											//     }
											// });
										}
										
									}).catch(err =>{
										console.log(err)
										_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? 'keystore 保存失败':'Keystore save failed')
									})
									
								}else{
									uni.hideLoading();
									_this.$CommonJS.showToast(res.data.message);
								}
								//加密保存
								/*Wallet.encrypt(_this.password,null).then((ksInforStr) => {
									console.log(ksInforStr);
									console.log( _this.accountName)
									//保存文件
									uni.setStorage({
									    key: _this.accountName,
									    data: ksInforStr,
									    success: function () {
												  uni.hideLoading();
									        console.log('success');
													uni.showToast({
														title:"密码已重置",
													})
													setTimeout(()=>{
														_this.$CommonJS.navigateTo('../login/login')
													},1000)
												
									    }
									});
								}).catch(error =>{
									uni.hideLoading();
									console.log(error.message);
								})*/
								
							},
							fail(err) {
								console.log(err)
							}
						})
						return;
					
				
				
				
				//this.$CommonJS.navigateTo('../login/login')
			},
			back(){
				this.$CommonJS.navigateBack()
			},
			login(){
				this.$CommonJS.navigateTo('../index/home')
			}
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/password/password.stylus"
	.input_placeholder
		font-size 12px 
		color rgba(255,255,255,0.3) 
		text-align right
	.xiaobai_check,.xiaobai_account
		font-size 12px
		color #FFFFFF
		width 434upx 
		margin-bottom 20upx
	
</style>
